<template>
  <div class="login">
    <van-nav-bar title="登录" />
    <van-form @submit="loginFn">
      <van-field
        required
        v-model="mobile"
        label="手机号"
        placeholder="请输入手机号"
        :rules="rules.mobile"
      />
      <van-field
        required
        v-model="code"
        type="number"
        label="验证码"
        placeholder="验证码"
        :rules="rules.code"
      />
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :disabled="isLoad"
          :loading="isLoad"
          loading-text="登录中..."
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
// import axios from '@/utils/request'
import { mapMutations } from 'vuex'
import { userLogin } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      mobile: '17521598716',
      code: '246810',
      isLoad: false,
      rules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确格式手机号' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码' }
        ]
      }
    }
  },
  methods: {
    ...mapMutations('user', ['setToken']),
    async loginFn() {
      this.isLoad = true
      console.log('发送中....')
      try {
        const res = await userLogin(this.mobile, this.code)
        console.log(res)
        this.$toast.success('登录成功')
        // this.$store.commit('user/setToken', res.data)
        this.setToken(res.data)
        this.$router.push('/')
      } catch (e) {
        console.log(e)
        this.$toast.fail('登录失败')
      }
      this.isLoad = false
    }
  }
}
</script>

<style></style>
